package utils;

import constants.HostConstant;
import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;

@Slf4j
public class WebDriverUtils {

    public WebDriver driver;

    protected WebDriverWait wait;

    protected Actions actions;

    protected final String LOGIN_PAGE_URL = "http://" + HostConstant.HOST + ":5173/user/login";
    protected static final String INIT_URL = "https://www.baidu.com";


    private WebDriver createDriver() {
        if (driver == null) {
            System.setProperty("wdm.chromeGoodVersionsUrl", "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json");
            System.setProperty("wdm.chromeLastGoodVersionsUrl", "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json");
            // 1. 下载浏览器驱动
            WebDriverManager.chromedriver().proxy("127.0.0.1:7897").useLocalCommandsPropertiesFirst().setup();
            // 2. 添加配置
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*", "--headless");
            driver = new ChromeDriver(options);
        }
        // 为所有元素查找添加隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        actions = new Actions(driver);
        return driver;
    }

    public WebDriverUtils(String url) {
        // 1. 创建浏览器驱动对象
        driver = createDriver();
        // 2. 根据传入的 url 打开网页
        driver.get(url);
    }


    public static void saveScreenShot(WebDriver driver, String className, String methodName) {
        // 1. 每次生成的屏幕截图都能保存，避免覆盖
        // 2. 屏幕截图文件保存到 test 目录下的 images 目录下，且所有生成的文件按日期放在不同的文件夹
        // 3. 每个生成的屏幕截图名称为: 当前方法名_时-分-秒-毫秒.jpg，
        // 示例: src/test/images/年-月-日/className/methodName_时-分-秒-毫秒.jpg
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH-mm-ss-SSS");
        String date = dateFormat.format(new Date());
        String time = timeFormat.format(new Date());

        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String destFilePath = String.format("./src/test/images/%s/%s/%s-%s.jpg", date, className, methodName, time);
        try {
            FileUtils.copyFile(screenshot, new File(destFilePath));
        } catch (IOException e) {
            log.error("保存屏幕照片失败!");
        }
    }


    protected void inputClearAndSend(WebElement elem, String text) {
        elem.sendKeys(Keys.CONTROL, "a");
        elem.sendKeys(text);
    }

}
